<?php

class Application_Model_Mapper_AenderungHafenMapper{
	protected $_dbTable;

	public function setDbTable ($dbTable){
		if (is_string($dbTable)){
			$dbTable = new $dbTable();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract){
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable (){
		if (null === $this->_dbTable){
			$this->setDbTable('Application_Model_DbTable_aenderunghafen');
		}
		return $this->_dbTable;
	}

	public function add (Application_Model_Hafen $object, $originalID){
		$now = new DateTime();
		/**
		 * Fill data array
		 */
		
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
			$data = array(
					'id' =>$generatedid,
					'originalID'=>$originalID,
					'created' => date('Y-m-d H:i:s'),
					'name' => $object->getName(),
					'lastupdater'=> $object->getLastupdater(), //id von ersteller mitschicken
					'lastupdated'=> date('Y-m-d H:i:s'),
					'lat' =>$object->getLat(),
					'lng' =>$object->getLng(),
					'tiefe' => $object->getTiefe(),
					'ankerplaetze' => $object->getAnkerplaetze(),
					'revier' => $object->getRevier(),
					'land' => $object -> getLand(),
					'stadt' => $object->getStadt(),
					'adresse' => $object->getAdresse(),
					'typ' => $object->getTyp(),
					'tankstelle' => $object->getTankstelle(),
					'parken' => $object->getParken(),
					'wasser' => $object->getWasser(),
					'strom' => $object->getStrom(),
					'toilette' => $object->getToilette(),
					'dusche' => $object->getDusche(),
					'reperatur' => $object->getReperatur(),
					'kran' => $object->getKran(),
					'slipping' => $object->getSlipping(),
					'supermarkt' => $object->getSupermarkt(),
					'waescherei' => $object->getWaescherei(),
					'travelLift' => $object->getTravelLift(),
					'wlan' => $object->getWlan(),
					'weblink' => $object->getWeblink(),
	// 				'koordinaten' => $object->getKoordinaten(),
// 					'premium' => $object->getPremium()
			// 				'profilbild' => $object->getProfilbild()
			);
			return $this->getDbTable()->insert($data);
		}
		else{
// 			$this->addHafen($object);
			$this->addHafen($object);
		}
	}

	public function deleteHafen ($id){
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?', $id);
		$table->delete($where);
	}

	public function fetchID ($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('aenderunghafen');
		$entry->where('aenderunghafen.id = ?', $id);
		return $entry->query()->fetch();
	}

	public function fetchAll(){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('aenderunghafen');
		return $entry->query()->fetchall();
	}
	
}